package model;

import entities.ViewShiftRequest;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.AbstractTableModel;

public class EditShiftRequestTableModel extends AbstractTableModel {

    ArrayList<ViewShiftRequest> list;

    public EditShiftRequestTableModel() {
        list = new ArrayList<ViewShiftRequest>();
    }

    public EditShiftRequestTableModel(ArrayList<ViewShiftRequest> list) {
        this.list = list;
    }

    @Override
    public int getRowCount() {
        return list.size();
    }

    @Override
    public int getColumnCount() {
        return 8;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        ViewShiftRequest view = list.get(rowIndex);
        String[] status = {"warning", "accept", "not accept"};        
        switch (columnIndex) {
            case 0:
                return view.getEmployeeName();
            case 1:
                return view.getDepartmentName();
            case 2:
                return view.getCreateDate();
            case 3:
                return view.getStartDate();
            case 4:
                return view.getEndDate();
            case 5:
                return view.getShiftName();
            case 6:
                return status[view.getStatus() - 1];
            case 7:
                return view.getModifiedDate();
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Name";
            case 1:
                return "Department";
            case 2:
                return "Create Date";
            case 3:
                return "Start Date";
            case 4:
                return "End Date";
            case 5:
                return "Shift";
            case 6:
                return "Status";
            case 7:
                return "ModifiedDate";
            default:
                return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return String.class;
            case 1:
                return String.class;
            case 2:
                return Date.class;
            case 3:
                return Date.class;
            case 4:
                return Date.class;
            case 5:
                return Integer.class;
            case 6:
                return String.class;
            case 7:
                return Date.class;
            default:
                return null;
        }
    }

    @Override
    public void fireTableDataChanged() {
        super.fireTableDataChanged();
    }

    public boolean addAll(ArrayList<ViewShiftRequest> list) {
        boolean returnVal = this.list.addAll(list);
        if (returnVal) {
            fireTableDataChanged();
        }
        return returnVal;
    }

    public boolean addElement(ViewShiftRequest view) {
        boolean returnVal = list.add(view);
        if (returnVal) {
            fireTableDataChanged();
        }
        return returnVal;
    }

    public ViewShiftRequest getAt(int index) {
        return list.get(index);
    }

    public void removeAt(int index) {
        list.remove(index);
    }

    public void update(int index, ViewShiftRequest view) {
        list.set(index, view);
        fireTableDataChanged();
    }
    
    public void update(ViewShiftRequest view) {
        list.set(list.indexOf(view), view);
        fireTableDataChanged();
    }

    public void clear() {
        list.clear();
    }
}
